package ATM.service.impl;

import ATM.dao.UserDAO;
import ATM.dao.impl.UserDAOImpl;
import ATM.frame.TransferPanel;
import ATM.pojo.UserDO;
import ATM.service.UserService;

public class UserServiceImpl implements UserService {
    private UserDAO userDAO = new UserDAOImpl();

    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        return userDAO.getByUsernameAndPwd(username, pwd);
    }

    @Override
    public void saveMoney(int id, int money) {
        UserDO user = userDAO.getById(id);
        user.setMoney(user.getMoney() + money);
        userDAO.update(user);
    }

    @Override
    public void getMoney(int id, int money) throws Exception {
        UserDO user = userDAO.getById(id);
        if (user.getMoney() < money) {
            throw new Exception("余额不足");
        }
        user.setMoney(user.getMoney() - money);
        userDAO.update(user);
    }

    @Override
    public void query(int id) {

    }


    @Override
    public void modify(int id, String pwd) {
        UserDO user = userDAO.getById(id);
        user.setUserPassWord(pwd);
        userDAO.update(user);
    }

    @Override
    public void transferMoney(int id1, String name, int money) throws Exception {
        UserDO user1 = userDAO.getById(id1);
        UserDO user2 = userDAO.getByName(name);

        if (user1.getUserName().equals(user2.getUserName())){
            throw new Exception("你不能转给自己");
        }
        if (user1.getMoney()<money){
            throw new Exception("余额不足，请充值");
        }else {
            user1.setMoney(user1.getMoney()-money);
            user2.setMoney(user2.getMoney()+money);
            userDAO.update(user1);
            userDAO.update(user2);
        }
    }


}
